package com.fly.controller;

import com.fly.bean.Users;
import com.fly.service.AddFixTaskService;
import com.fly.service.UpdateFixTaskService;
import com.fly.util.Msg;
import com.fly.vo.AddFix_WH;
import com.fly.vo.AlterFix_WH;
import com.fly.vo.AuditFix_WH;
import com.fly.vo.UpdateFix_WH;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UpdateFixTaskController {

    @Resource
    private UpdateFixTaskService updateFixTaskService;

    @Resource
    private AddFixTaskService addFixTaskService;

    //查询任务对应的消缺员
    @RequestMapping("/selectFixUserByFixId")
    @ResponseBody
    public Msg selectFixUserByFixId(int fixId) {
        List<Users> users = updateFixTaskService.selectFixUserByFixId(fixId);

        return Msg.success().add("users", users);
    }

    //     查询待选的消缺员
    @RequestMapping("/selectWaitFixUserByFixId")
    @ResponseBody
    public Msg selectWaitFixUserByFixId(int fixId) {
        List<Users> users = updateFixTaskService.selectWaitFixUserByFixId(fixId);

        return Msg.success().add("users", users);
    }

    //给任务分配消缺员
    @RequestMapping("/updateTaskUserRelation")
    @ResponseBody
    public Msg updateTaskUserRelation(UpdateFix_WH updateFix_wh) {

        int fixId = updateFix_wh.getFixId();
        int taskState = updateFixTaskService.selectTaskStateByFixId(fixId);

        int result = 0;
        //如果任务状态为未分配，然后添加了分配人员则修改任务状态为已分配
        //如果任务状态为已分配，修改时又删除了分配人员则修改任务状态为未分配
        if (taskState == 2) {
            //如果状态为已分配 先删除关系
            result = updateFixTaskService.deleteRelationByFixId(fixId);

            if (updateFix_wh.getFixUserId() == "") {
                updateFix_wh.setSysProValueId(1);
                updateFixTaskService.updateTaskState(updateFix_wh);
            }
        } else if (taskState == 1) {
            if (updateFix_wh.getFixUserId() != "") {
                updateFix_wh.setSysProValueId(2);
                updateFixTaskService.updateTaskState(updateFix_wh);
            }
        }

//        获取需要添加的消缺员的Id 添加关系
        int result1 = 0;
        if (updateFix_wh.getFixUserId() != "") {
            String[] fixUserIds = updateFix_wh.getFixUserId().split(",");
            for (String s : fixUserIds) {
                updateFix_wh.getFixUserIds().add(Integer.valueOf(s));
            }

            result1 = updateFixTaskService.addTaskUserRelation(updateFix_wh);
        }


        return Msg.success().add("msg", "分配任务成功,删除关系" + result + "条" + "   添加任务关系" + result1 + "条");
    }


    //    取消任务
    @RequestMapping("/updateTaskIsCancel")
    @ResponseBody
    public Msg updateTaskIsCancel(UpdateFix_WH updateFix_wh) {

//        修改任务状态为取消
        updateFix_wh.setIsCancel(0);
        int result = updateFixTaskService.updateTaskIsCancel(updateFix_wh);

        int fixId = updateFix_wh.getFixId();


        // 释放该任务下的消缺员 已分配的任务才需要释放
        int taskState = updateFixTaskService.selectTaskStateByFixId(fixId);

        int result1 = 0;
        String result5 = "无需修改任务状态";
        if (taskState == 2) {
            result1 = updateFixTaskService.deleteRelationByFixId(fixId);
//            任务取消状态改为未分配
            updateFix_wh.setSysProValueId(1);
            updateFixTaskService.updateTaskState(updateFix_wh);
            result5 = "任务状态改为未分配";

        }

        //同时修改电线杆状态由已分配改为未解决
        AddFix_WH addFix_wh = new AddFix_WH();
        addFix_wh.setDamageState(0);
        addFix_wh.setFixId(fixId);
        int result3 = addFixTaskService.updatePoleDamageState(addFix_wh);

        //最后释放该任务下的电线杆
        int result2 = updateFixTaskService.deletePoleFixRelation(fixId);


        return Msg.success().add("msg", "修改任务状态为取消成功" + "     " +
                "释放该任务下的消缺员" + result1 + "个         " +
                result5 +
                "        释放该任务下的电线杆" + result2 + "个          " +
                "修改电线杆状态由已分配改为未解决成功");

    }

    //开启任务
    @RequestMapping("/startTaskIsCancel")
    @ResponseBody
    public Msg startTaskIsCancel(UpdateFix_WH updateFix_wh) {

        updateFix_wh.setIsCancel(1);
        int result = updateFixTaskService.updateTaskIsCancel(updateFix_wh);


        return Msg.success().add("msg", "开启任务成功");
    }


    //    修改消缺任务
    @RequestMapping("/alterFixTaskAll")
    @ResponseBody
    public Msg alterFixTaskAll(AlterFix_WH alterFix_wh) {
        int fixId = alterFix_wh.getFixId();

        UpdateFix_WH updateFix_wh = new UpdateFix_WH();
        updateFix_wh.setFixId(fixId);
        AddFix_WH addFix_wh = new AddFix_WH();
        addFix_wh.setFixId(fixId);

        //如果任务状态为未分配，然后添加了分配人员则修改任务状态为已分配
        //如果任务状态为已分配，修改时又删除了分配人员则修改任务状态为未分配
        int taskState = updateFixTaskService.selectTaskStateByFixId(fixId);
        if (taskState == 1) {
            if (alterFix_wh.getFixUserId() != "") {
                updateFix_wh.setSysProValueId(2);
                updateFixTaskService.updateTaskState(updateFix_wh);
            }
        } else if (taskState == 2) {
            if (alterFix_wh.getFixUserId() == "") {
                updateFix_wh.setSysProValueId(1);
                updateFixTaskService.updateTaskState(updateFix_wh);
            }
        }


//     修改任务Task
        int result = updateFixTaskService.updateTask(alterFix_wh);

//        修改消缺任务单Fix
        int result1 = updateFixTaskService.updateFix(alterFix_wh);

//        修改消缺员与任务的关系 调用 selectTaskStateByFixId deleteRelationByFixId addTaskUserRelation
        int result2 = 0;
        if (taskState == 2) {
            result2 = updateFixTaskService.deleteRelationByFixId(fixId);
        }

//        获取需要添加的消缺员的Id 添加关系
        if (alterFix_wh.getFixUserId() != "") {
            String[] fixUserIds = alterFix_wh.getFixUserId().split(",");
            for (String s : fixUserIds) {
                alterFix_wh.getFixUserIds().add(Integer.valueOf(s));
            }
            updateFix_wh.setFixUserIds(alterFix_wh.getFixUserIds());
            updateFix_wh.setFixId(fixId);
            int result3 = updateFixTaskService.addTaskUserRelation(updateFix_wh);
        }


//        修改电线杆状态
        addFix_wh.setDamageState(0);
        int result4 = addFixTaskService.updatePoleDamageState(addFix_wh);

//        修改消缺任务与电线杆的关系 调用 addPoleFixRelation deletePoleFixRelation
        int result5 = updateFixTaskService.deletePoleFixRelation(fixId);

        addFix_wh.setDamageState(1);
        String[] poleIds = alterFix_wh.getDamageRecordId().split(",");
        for (String s : poleIds) {
            addFix_wh.setDamageRecordId(s);
            addFixTaskService.addPoleFixRelation(addFix_wh);
        }

//        修改电线杆状态
        addFix_wh.setDamageState(1);
        int result6 = addFixTaskService.updatePoleDamageState(addFix_wh);


        return Msg.success().add("msg", "修改任务成功,   删除消缺员与任务的关系" + result2 + "个" +
                "        添加消缺员与任务的关系" + alterFix_wh.getFixUserId().split(",").length + "个" +
                "         修改任务之前的电线杆状态" + result4 + "个" +
                "         删除消缺任务与电线杆的关系" + result5 + "个" +
                "        添加消缺任务与电线杆的关系" + poleIds.length + "个" +
                "         修改任务新分配的电线杆状态" + result6 + "个");
    }


    //    负责人审核
    @RequestMapping("/manageAuditTask")
    @ResponseBody
    public Msg manageAuditTask(AuditFix_WH auditFix_wh) {
        updateFixTaskService.manageUserAuditTask(auditFix_wh);
        return Msg.success().add("msg", "负责人审核成功");
    }

    //    下发人审核
    @RequestMapping("/creatAuditTask")
    @ResponseBody
    public Msg creatAuditTask(AuditFix_WH auditFix_wh) {

        UpdateFix_WH updateFix_wh = new UpdateFix_WH();
        AddFix_WH addFix_wh = new AddFix_WH();

        updateFix_wh.setFixId(auditFix_wh.getFixId());
        addFix_wh.setFixId(auditFix_wh.getFixId());
        if (auditFix_wh.getPassAudit() == 1) {
            updateFix_wh.setSysProValueId(5);
            addFix_wh.setDamageState(2);
        } else if (auditFix_wh.getPassAudit() == 0) {
            updateFix_wh.setSysProValueId(6);
            addFix_wh.setDamageState(0);
        }

        //修改任务下的电线杆状态
        addFixTaskService.updatePoleDamageState(addFix_wh);
        //审核之后修改任务状态
        updateFixTaskService.updateTaskState(updateFix_wh);
        //给消缺任务设置审核信息
        updateFixTaskService.creatUserAuditTask(auditFix_wh);

        return Msg.success().add("msg", "下发人审核成功");
    }

    //    即使下发人又是负责人审核
    @RequestMapping("/bossAuditTask")
    @ResponseBody
    public Msg bossAuditTask(AuditFix_WH auditFix_wh) {

        UpdateFix_WH updateFix_wh = new UpdateFix_WH();
        AddFix_WH addFix_wh = new AddFix_WH();

//        审核之后修改状态
        updateFix_wh.setFixId(auditFix_wh.getFixId());
        addFix_wh.setFixId(auditFix_wh.getFixId());
        if (auditFix_wh.getPassAudit() == 1) {
            updateFix_wh.setSysProValueId(5);
            addFix_wh.setDamageState(2);
        } else if (auditFix_wh.getPassAudit() == 0) {
            updateFix_wh.setSysProValueId(6);
            addFix_wh.setDamageState(0);
        }

        //修改任务下的电线杆状态
        addFixTaskService.updatePoleDamageState(addFix_wh);
        //审核之后修改任务状态
        updateFixTaskService.updateTaskState(updateFix_wh);
        //给消缺任务设置审核信息
        updateFixTaskService.bossAuditTask(auditFix_wh);

        return Msg.success().add("msg", "下发人,负责人审核成功");
    }
}